package Testing;

import static org.testng.AssertJUnit.assertTrue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.testng.annotations.AfterClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.AssertJUnit;
import com.thoughtworks.selenium.*;

public class ExcelDrivenAutomation {
	private static Selenium selenium;
	public String url = "http://172.16.1.88:8431/";
	public String[] Headers = null;

	@BeforeClass
	public void startSelenium() {
		selenium = new DefaultSelenium("localhost", 4444,
				"*chrome C:\\Program Files\\Mozilla Firefox\\firefox.exe", url);
		selenium.start();
		selenium.open(url);
		selenium.windowFocus();
		selenium.windowMaximize();
	}

	@DataProvider(name = "DP2")
	public Object[][] createData2() {

		ArrayList<Object> retObjArr = null;
		tableArray tA = new tableArray();

		retObjArr = tA.getTableArray("C:\\Users\\mkasim\\Desktop\\Login.xls",
				"Sheet1", "Sales by Product");
		Headers = (String[]) retObjArr.get(0);
		Object[][] Data = (Object[][]) retObjArr.get(1);
		Concatenate cc = new Concatenate();
		Object[][] DData = null;
		DData = cc.concat(Data);
		return (DData);
	}

	public static void actions(String[] data1, String[] headers2)
			throws Exception {
		Links lk = new Links();
		isNumber in = new isNumber();
		HashMap<String, String> hm = lk.links();
		HashMap<String, Integer> links = new HashMap<String, Integer>();
		links.put("dropdownbox", 1);
		links.put("radiobutton", 2);
		links.put("link", 3);
		links.put("textbox", 4);
		
		for (int i = 0; i < headers2.length; i++) {
			System.out.println("headers2["+i+"]"+headers2[i]);
			int link = links.get(hm.get(headers2[i]).split(";")[0]);

			switch (link) {
				case 1:
					selenium.select(headers2[i], "label=" + data1[i]);
					break;
				case 2:
					/*System.out.println("headers2["+i+"] =" + headers2[i]);*/
					selenium.click(headers2[i]);
					break;
				case 3:
					selenium.click(headers2[i]);
					if(hm.get(headers2[i]).split(";")[1]!=null)
					{
					selenium.waitForPageToLoad(hm.get(headers2[i]).split(";")[1]);
					}
					break;
				case 4:
					selenium.type("id=" + headers2[i], data1[i]);
					break;
			}
		}
	}

	@Test(dataProvider = "DP2")
	public void testReportCreation(String Data) throws Exception {
		Concatenate ct = new Concatenate();
		String[] Data1 = ct.split(Data);
		ExcelDrivenAutomation.actions(Data1, Headers);
		assertTrue(selenium.getConfirmation().matches("^Are you sure, you want to delete INDIAKarnatakaBangaloreIngram micro india ltd\\.FY 2011FY 2012[\\s\\S]$"));
		selenium.waitForPageToLoad("30000");
		selenium.click("logout");
		selenium.waitForPageToLoad("30000");
		
	}

	@AfterClass(alwaysRun = true)
	public void stopSelenium() {
		this.selenium.stop();
	}

	public void sleepstate(String state) {
		int count = 1;
		while (count < 50) {
			String titlestate = selenium.getTitle();
			if (titlestate.equals(state)) {
			} else {
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			count = count + 1;
		}
	}
}
